/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.example.common.config;

import apijson.JSONRequest;
import apijson.RequestMethod;
import apijson.framework.APIJSONParser;
import apijson.framework.APIJSONVerifier;
import apijson.orm.SQLConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**安全校验器，校验请求参数、角色与权限等
 * 具体见 https://github.com/Tencent/APIJSON/issues/12
 * @author Lemon
 */
public class IcpVerifier extends APIJSONVerifier<String> {
	public static final String TAG = "IcpVerifier";



	/*@Override
	public String getVisitorIdKey(apijson.orm.SQLConfig config) {
		return "ownerId";
	}*/

	/*@Override
	public boolean verifyAccess(SQLConfig config) throws Exception {
		// 过滤预校验
		if (config.getTable()==null||
				config.getTable().equals("Function") && config.getMethod() == RequestMethod.HEAD ||
				config.getTable().equals("Function") && config.getMethod() == RequestMethod.GET ||
				config.getTable().equals("Access") && config.getMethod() == RequestMethod.HEAD ||
				config.getTable().equals("Access") && config.getMethod() == RequestMethod.GET ||
				config.getTable().equals("Request") && config.getMethod() == RequestMethod.GET ||
				config.getTable().equals("Request") && config.getMethod() == RequestMethod.HEAD) {
			return true;
		}

		// 获取前端传来的角色
		String role = config.getRole();

		// 当前用户未登录
		if (role.equals("UNKNOWN") || SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser")) {
			return super.verifyAccess(config);
		}
		// 获取当前用户
		SecureUser secureUser = (SecureUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		// 启用自定义权限验证
		if(!(role.equals("UNKNOWN")||role.equals("LOGIN")||role.equals("CONTACT")||role.equals("CIRCLE")||role.equals("OWNER")||role.equals("ADMIN"))){
			// 获取当前用户的所有信息
			JSONObject jsonObject = new APIJSONParser(RequestMethod.GET, false).parseResponse(
					new JSONRequest("[]", new apijson.JSONObject("")
							.puts("join","</Sys_user_role/user_id@,</Sys_role/id@")
							.puts("Sys_user",new apijson.JSONObject("").puts("id",secureUser.getId()))
							.puts("Sys_user_role",new apijson.JSONObject("").puts("user_id@","/Sys_user/id"))
							.puts("Sys_role",new apijson.JSONObject("").puts("id@","/Sys_user_role/role_id"))
					)
			);

			// 获取当前用户对应的角色
			List<JSONObject> list=new ArrayList<>();
			JSONArray jsonArray = jsonObject.getJSONArray("[]");

			// 利用stream流获取用户的所有权限角色
			jsonArray.stream().filter(Objects::nonNull).forEach(dist->{
				list.add(((JSONObject) dist).getJSONObject("Sys_role"));
			});

			// 循环判断当前角色的role是否符合条件
			for(JSONObject sysRole:list){
				if(sysRole.getString("name").equals(role)){
					return true;
				}
			}
			throw new RuntimeException("角色异常");
		}

		super.verifyAccess(config);

		return false;
	}*/

}
